<?php
/**
 *
 * Login(后台登陆页面)
 *
 */
class LoginAction extends BaseAction {
	function _initialize() {
		parent::_initialize();
	}
	
	public function index() {
		if (!empty($_SESSION['uid'])&&!empty($_SESSION['uname'])) {
			$this->assign ( 'jumpUrl', U ( 'User/index' ) );
			$this->success ( L ( 'logined' ) );
		}
		$this->display ();
	}
	
	public function reg() {
		if (!empty($_SESSION['uid'])&&!empty($_SESSION['uname'])) {
			$this->assign ( 'jumpUrl', U ( 'User/index' ) );
			$this->success ( L ( 'logined' ) );
		}
		$this->display ();
	}
	
	/**
	   *用户注册
	   *@date 2011-7-27
	   *@time 下午12:10:36
	   */
	function doreg() {
		//用户注册
		if (! $_POST [C ( 'TOKEN_NAME' )] || $_POST [C ( 'TOKEN_NAME' )] != $_SESSION [C ( 'TOKEN_NAME' )]) {
			$this->error ( L ( '_TOKEN_ERROR_' ) );
		}
		$username = trim ( $_POST ['username'] );
		$password = trim ( $_POST ['password'] );
		$verifyCode = trim ( $_POST ['verifyCode'] );
		
		if (empty ( $username ) || empty ( $password )) {
			$this->error ( L ( 'empty_username_empty_password' ) );
		} elseif (md5 ( $verifyCode ) != $_SESSION ['verify']) {
			$this->error ( L ( 'error_verify' ) );
		}
		if($password!=$_POST['repassword']){
			$this->error ('两次密码不一致');
		}
		if(empty($_POST['email'])){
			$this->error ('邮箱地址必须填写');
		}
		$user=D("Biz.Member");
		$data = array ();
		$data=$user->create();
		if($data){
			if(false!==$user->add()){
				$uid=$user->getLastInsID();		
				//$this->success(L('add_ok'));
			}else{
				$this->error(L('add_error'));
			}
		}else{
			$this->error($user->getError());
		}
		$authInfo = $user->where (array('id'=>$uid))->find ();
		
		$_SESSION ['uname'] = $authInfo ['username'];
		$_SESSION ['uid'] = $authInfo ['id'];
		$_SESSION ['is_ltd'] = $authInfo ['is_ltd'];
		$_SESSION ['rid'] = $authInfo ['rid'];
		$_SESSION ['tid'] = $authInfo ['tid'];
		$_SESSION ['email'] = $authInfo ['email'];
		$_SESSION ['lastLoginTime'] = $authInfo ['last_logintime'];
		$_SESSION ['login_count'] = $authInfo ['login_count'] + 1;
		
		//保存登录信息
		$data = array ();
		$data ['id'] = $authInfo ['id'];
		$data ['last_logintime'] = time ();
		$data ['last_ip'] = get_client_ip ();
		$data ['login_count'] = array ('exp', 'login_count+1' );
		$user->save ( $data );
		
		// 缓存访问权限
		$this->ajaxReturn ( $authInfo,'注册成功', 1 );
	}//end doreg
	
	/**
	 * 提交登录
	 *
	 */
	public function doLogin() {
		
		if (! $_POST [C ( 'TOKEN_NAME' )] || $_POST [C ( 'TOKEN_NAME' )] != $_SESSION [C ( 'TOKEN_NAME' )]) {
			$this->error ( L ( '_TOKEN_ERROR_' ) );
		}
		unset ( $_POST ['__hash__'] );
		$username = trim ( $_POST ['username'] );
		$password = trim ( $_POST ['password'] );
		$verifyCode = trim ( $_POST ['verifyCode'] );
		
		if (empty ( $username ) || empty ( $password )) {
			$this->error ( L ( 'empty_username_empty_password' ) );
		} elseif (md5 ( $verifyCode ) != $_SESSION ['verify']) {
			$this->error ( L ( 'error_verify' ) );
		}
		
		$condition = array ();
		$condition ['username'] = $username;
		$condition ['is_ltd'] = $_POST['is_ltd'];
		$member = M ( "Member" );
		$authInfo = $member->where ( $condition )->find ();
		//dump($member->getLastSql());
		//使用用户名、密码和状态的方式进行认证
		if (false === $authInfo) {
			$this->error ( L ( 'empty_userid' ) );
		} else {
			if ($authInfo ['password'] != sysmd5 ( $_POST ['password'] )) {
				$this->error ( L ( 'password_error' ) );
				//$this->error ( sysmd5 ( $_POST ['password'] ) );
			}
			
			$_SESSION ['uname'] = $authInfo ['username'];
			$_SESSION ['uid'] = $authInfo ['id'];
			$_SESSION ['is_ltd'] = $authInfo ['is_ltd'];
			$_SESSION ['rid'] = $authInfo ['rid'];
			$_SESSION ['tid'] = $authInfo ['tid'];
			$_SESSION ['email'] = $authInfo ['email'];
			$_SESSION ['lastLoginTime'] = $authInfo ['last_logintime'];
			$_SESSION ['login_count'] = $authInfo ['login_count'] + 1;
			
			//保存登录信息
			$dao = M ( 'Member' );
			$data = array ();
			$data ['id'] = $authInfo ['id'];
			$data ['last_logintime'] = time ();
			$data ['last_ip'] = get_client_ip ();
			$data ['login_count'] = array ('exp', 'login_count+1' );
			$dao->save ( $data );
			
			// 缓存访问权限
			$this->ajaxReturn ( $authInfo, L ( 'login_ok' ), 1 );
		}
	}
	
	/**
	 * 验证码
	 *
	 */
	public function verify() {
		$type = isset ( $_GET ['type'] ) ? $_GET ['type'] : 'gif';
		import ( "@.ORG.Image" );
		Image::buildImageVerify ( 4, 1, $type );
	}
	
	/**
	 * 退出登录
	 *
	 */
	public function logout() {
		if (isset ( $_SESSION ['uid'] )) {
			unset ( $_SESSION ['uid'] );
			unset ( $_SESSION );
			session_destroy ();
			$this->assign ( 'jumpUrl', U ( 'Login/index' ) );
			$this->success ( L ( 'loginouted' ) );
		} else {
			$this->assign ( 'jumpUrl', U ( 'Login/index' ) );
			$this->error ( L ( 'logined' ) );
		}
	}
	
	function checkEmail() {
		$user = M ( 'Member' );
		$email = $_GET ['email'];
		$userid = intval ( $_GET ['userid'] );
		if (empty ( $userid )) {
			if ($user->getByEmail ( $email )) {
				echo 'false';
			} else {
				echo 'true';
			}
		} else {
			//判断邮箱是否已经使用
			if ($user->where ( "id!={$userid} and email='{$email}'" )->find ()) {
				echo 'false';
			} else {
				echo 'true';
			}
		}
		exit ();
	}
}
